package com.qiu.wxapi.controller;

import com.qiu.wxapi.pojo.SignIn;
import com.qiu.wxapi.service.SignInService;
import com.qiu.wxapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("sign")
@ResponseBody
public class SignInController {


    @Autowired
    private SignInService signInService;
    @Autowired
    private UserService userService;

    /**
     * 保存打卡信息并回显
     *
     * @param signIn
     * @return
     */
    @PostMapping
    public ResponseEntity<List<SignIn>> save(@RequestBody SignIn signIn) {


        this.signInService.insertSelective(signIn);
        List<SignIn> signIns = this.signInService.selectAll(signIn.getUserId());
        if (CollectionUtils.isEmpty(signIns)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(signIns);
    }

    /**
     * 查询当天的打卡信息
     *
     * @return
     */
    @GetMapping
    public ResponseEntity<List<SignIn>> findAll(@RequestParam("userId") Integer userId) {
        List<SignIn> signIns = this.signInService.selectAll(userId);
        if (CollectionUtils.isEmpty(signIns)) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.ok(signIns);
    }

}
